package com.ityy.ruiji.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ityy.ruiji.common.R;
import com.ityy.ruiji.dao.CategoryDao;
import com.ityy.ruiji.dao.DishDao;
import com.ityy.ruiji.dao.DishFlavorDao;
import com.ityy.ruiji.entity.*;
import com.ityy.ruiji.entity.dto.DishDto;
import com.ityy.ruiji.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl implements DishService {

    @Autowired(required = false)
    private DishDao dishDao;

    @Autowired(required = false)
    private DishFlavorDao dishFlavorDao;

    @Autowired(required = false)
    private CategoryDao categoryDao;

    @Override
    public R<Page> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Dish> dishDaoList = dishDao.findByName(name);
        PageInfo<Dish> pageInfo = new PageInfo(dishDaoList);
        List<DishDto> collect = pageInfo.getList().stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            Category byId = categoryDao.findById(dish.getCategoryId());
            dishDto.setCategoryName(byId.getName());
            return dishDto;
        }).collect(Collectors.toList());
        Page<DishDto> page1 = new Page(collect, pageInfo.getTotal());
        return R.success(page1);
    }

    @Override
    @Transactional
    public R save(DishDto dishDto) {
        //1. 补全dish的参数
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setSort(0);
        dishDao.insert(dishDto);
        List<DishFlavor> flavorList = dishDto.getFlavors();
        if (flavorList != null && flavorList.size() > 0) {
            flavorList = flavorList.stream().map(dishFlavor -> {
                dishFlavor.setDishId(dishDto.getId()); //所属的菜品的id
                dishFlavor.setCreateUser(dishDto.getCreateUser());
                dishFlavor.setUpdateUser(dishDto.getUpdateUser());
                dishFlavor.setCreateTime(dishDto.getCreateTime());
                dishFlavor.setUpdateTime(dishDto.getUpdateTime());
                return dishFlavor;
            }).collect(Collectors.toList());
        }
        dishFlavorDao.batchInsert(flavorList);
        return R.success("添加成功");
    }

    @Override
    public R findById(Long id) {
        Dish dish = dishDao.findById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        List<DishFlavor> dishFlavorList = dishFlavorDao.findById(id);
        dishDto.setFlavors(dishFlavorList);

        return R.success(dishDto);
    }

    @Override
    public R updateById(DishDto dishDto) {
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDao.update(dishDto);
        dishFlavorDao.deleteByDishId(dishDto.getId());
        List<DishFlavor> flavorList = dishDto.getFlavors();
        if (flavorList != null && flavorList.size() > 0) {
            flavorList = flavorList.stream().map(dishFlavor -> {
                dishFlavor.setDishId(dishDto.getId()); //所属的菜品的id
                dishFlavor.setCreateUser(dishDto.getCreateUser());
                dishFlavor.setUpdateUser(dishDto.getUpdateUser());
                dishFlavor.setCreateTime(dishDto.getCreateTime());
                dishFlavor.setUpdateTime(dishDto.getUpdateTime());
                return dishFlavor;
            }).collect(Collectors.toList());
        }


            dishFlavorDao.batchInsert(flavorList);
            return R.success(dishDto);

        }

    //根据菜品的类别id查询菜品列表
    @Override
    public R findId(Long categoryId,Integer status) {
        List<Dish> dishList =  dishDao.list(categoryId,status);
        //使用stream流遍历dishList，把每一个Dish转换为DishDto
        List<DishDto> dishDtoList = dishList.stream().map(dish -> {
            DishDto dishDto = new DishDto();
            //属性拷贝
            BeanUtils.copyProperties(dish, dishDto);
            //查询出这个菜品的口味
            List<DishFlavor> dishFlavorList = dishFlavorDao.findById(dish.getId());
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtoList);
    }
}
